#coding=utf-8
# 导入模板
import pygame
import time
from pygame.locals import *



# 子弹类

# 飞机类
class Plane(object):
    # 构造函数
    def __init__(self, side):
        # False为敌机
        self.side = side
        # 坐标
        self.x = 230
        self.y = 400
        # 图片
        self.image = pygame.image.load("./images/player.png")
        
    def keyControl(self, key):
        if key == K_UP :
            if self.y != 0 :
                self.y -= 10

        elif key == K_DOWN :
            if self.y != 480 :
                self.y += 10

        elif key == K_LEFT :
            if self.x != 0 :
                self.x -= 10

        elif key == K_RIGHT :
            if self.x != 290 :
                self.x += 10


# 飞机大战游戏
if __name__ == '__main__':
    # 创建窗体，设置大小
    window = pygame.display.set_mode((390, 600), 0, 32)
    # 加载背景图片
    background = pygame.image.load("./images/background.png")
    # 创建玩家飞机
    player = Plane(True)
   
    while True:
        # 将背景图片添加到窗体
        window.blit(background, (0, 0))
        # 将战机图片添加到窗体
        window.blit(player.image, (player.x, player.y))
        # 事件
        for event in pygame.event.get():
            if event.type == QUIT :
                exit()
            elif event.type == KEYDOWN :
                player.keyControl(event.key)
                
        # 更新窗口
        pygame.display.update()
        # 睡眠
        time.sleep(0.01)